package controllers;

import java.util.Date;
import java.util.List;
import java.util.Map;

import models.DrCase;
import models.SystemOperator;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aton.db.Config;

import services.CaseService;
import services.LogService;
import services.SystemParamService;
import utils.BizConstants;
import utils.ShellUtil;
import utils.SystemTool;
import vos.ReturnCode;

/**
 * 语音
 * @author heweikang
 *
 */
public class VoicePlatformController extends BaseController {
	
	public static final Logger shelllog=LoggerFactory.getLogger("shell");
	
	public static void voicePlatformManage(){
		SystemOperator oper=getLoginUser();
		renderArgs.put("roleid", oper.getRoleId());
		render();
	}
	
	public static void getSystemUsage(){
		renderJSON(SystemTool.getResourceInfoUsage());
	}
	/**
	 * 重启语音识别引擎
	 */
	public static void doRestartAsr(){
		String asrShell=SystemParamService.getValueByCacheId(BizConstants.PARAM_MAP.get("SSH_RESTART_ASR_SHELL"));
		String asrip=SystemParamService.getValueByCacheId(BizConstants.PARAM_MAP.get("SSH_RESTART_ASR_IP"));
		String asruser=SystemParamService.getValueByCacheId(BizConstants.PARAM_MAP.get("SSH_RESTART_ASR_USER"));
		String asrpass=SystemParamService.getValueByCacheId(BizConstants.PARAM_MAP.get("SSH_RESTART_ASR_PASS"));
		if(StringUtils.isEmpty(asrShell)){
			renderRespondJson(ReturnCode.ErrorCode, "重启引擎脚本不存在");
		}
		//获取活跃案件（休庭和开庭）
		List<DrCase> cases=CaseService.getLivingCase();
		if(cases!=null&&cases.size()>0){
			renderRespondJson(ReturnCode.ErrorCode, "有案件正在执行中，不允许进行重启");
		}
		String shellTxt="cd "+asrShell+"&& ./restart_asr.sh";
		shelllog.info("重启语音识别引擎执行命令为："+shellTxt);
		//调用清空接口
		Map<String,Object> shellMap=ShellUtil.execAndPass(asrip,asruser,asrpass,shellTxt);
		if(shellMap==null){
			renderRespondJson(ReturnCode.ErrorCode, "重启语音识别引擎失败");
		}
		int shellStatus=(int)shellMap.get("status");
		if(shellStatus!=0){
			shelllog.info("重启语音识别引擎执行失败,错误输出为："+(String)shellMap.get("outErr"));
			renderRespondJson(ReturnCode.ErrorCode, "重启语音识别引擎失败");
		}
		LogService.saveLog(getLoginUser().getOperatorid(), request.remoteAddress,
				"重启语音识别引擎", "重启语音识别引擎成功", new Date());
		renderRespondJson(ReturnCode.SuccessCode, "重启成功");
	}
	/**
	 * 重启语音播报引擎
	 */
	public static void doRestartTts(){
		
		String ttsShell=SystemParamService.getValueByCacheId(BizConstants.PARAM_MAP.get("SSH_RESTART_TTS_SHELL"));
		String ttsip=SystemParamService.getValueByCacheId(BizConstants.PARAM_MAP.get("SSH_RESTART_TTS_IP"));
		String ttsuser=SystemParamService.getValueByCacheId(BizConstants.PARAM_MAP.get("SSH_RESTART_TTS_USER"));
		String ttspass=SystemParamService.getValueByCacheId(BizConstants.PARAM_MAP.get("SSH_RESTART_TTS_PASS"));
		if(StringUtils.isEmpty(ttsShell)){
			renderRespondJson(ReturnCode.ErrorCode, "重启引擎脚本不存在");
		}
		String shellTxt="cd "+ttsShell+" && ./restart_tts.sh";
		shelllog.info("重启语音播报引擎执行命令为："+shellTxt);
		Map<String,Object> shellMap=ShellUtil.execAndPass(ttsip,ttsuser,ttspass,shellTxt);
		if(shellMap==null){
			renderRespondJson(ReturnCode.ErrorCode, "重启语音播报引擎失败");
		}
		int shellStatus=(int)shellMap.get("status");
		if(shellStatus!=0){
			shelllog.info("重启语音播报引擎执行失败,错误输出为："+(String)shellMap.get("outErr"));
			renderRespondJson(ReturnCode.ErrorCode, "重启语音播报引擎失败");
		}
		LogService.saveLog(getLoginUser().getOperatorid(), request.remoteAddress,
				"重启语音播报引擎", "重启语音播报引擎成功", new Date());
		renderRespondJson(ReturnCode.SuccessCode, "重启成功");
	}
	/**
	 * 重启服务器
	 * @param password 管理员密码
	 */
	public static void doReboot(String password){
		SystemOperator oper=getLoginUser();
		if(!BizConstants.ADMIN_ROLE.equals(oper.getRoleId())&&!BizConstants.SERVICE_ROLE.equals(oper.getRoleId())){
			renderRespondJson(ReturnCode.ErrorCode, "您没有此操作权限");
		}
		if(!validate(oper.getPassword(),oper.getSalt(),password)){
			renderRespondJson(ReturnCode.ErrorCode, "管理员密码不正确");
		}
		//获取活跃案件（休庭和开庭）
		List<DrCase> cases=CaseService.getLivingCase();
		if(cases!=null&&cases.size()>0){
			renderRespondJson(ReturnCode.ErrorCode, "有案件正在执行中，不允许进行重启");
		}
		String rebootip=SystemParamService.getValueByCacheId(BizConstants.PARAM_MAP.get("SSH_REBOOT_IP"));
		String rebootuser=SystemParamService.getValueByCacheId(BizConstants.PARAM_MAP.get("SSH_REBOOT_USER"));
		String rebootpass=SystemParamService.getValueByCacheId(BizConstants.PARAM_MAP.get("SSH_REBOOT_PASS"));
		String shellTxt="reboot";
		shelllog.info("重启服务执行命令为："+shellTxt);
		Map<String,Object> shellMap=ShellUtil.execAndPass(rebootip,rebootuser,rebootpass,shellTxt);
		if(shellMap==null){
			renderRespondJson(ReturnCode.ErrorCode, "重启系统失败");
		}
		int shellStatus=(int)shellMap.get("status");
		if(shellStatus!=0){
			shelllog.info("重启服务执行失败,错误输出为："+(String)shellMap.get("outErr"));
			renderRespondJson(ReturnCode.ErrorCode, "重启系统失败");
		}
		LogService.saveLog(getLoginUser().getOperatorid(), request.remoteAddress,
				"重启服务", "重启服务器", new Date());
		renderRespondJson(ReturnCode.SuccessCode, "重启成功");
	}
}
